package cn.wolfcode.web.controller;


import cn.wolfcode.query.CustomerReportQueryObject;
import cn.wolfcode.service.ICustomerReportService;
import cn.wolfcode.util.MessageUtil;
import com.alibaba.fastjson.JSON;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("/customerReport")
public class CustomerReportController {

	@Autowired
	private ICustomerReportService customerReportService;

	@RequestMapping("/listByPie")
	public String listByPie(@ModelAttribute("qo") CustomerReportQueryObject qo,Model model){

		PageInfo<HashMap> pageInfo = customerReportService.selectCustomerReport(qo);
		List<HashMap> list = pageInfo.getList();

		List legend = new ArrayList<>();
		List series = new ArrayList<>();

		for (HashMap hashMap : list) {
			legend.add(hashMap.get("groupType"));
			Map<String,Object> map = new HashMap<>();
			map.put("name",hashMap.get("groupType"));
			map.put("value",hashMap.get("number"));
			series.add(map);
		}
		model.addAttribute("legend", JSON.toJSONString(legend));
		model.addAttribute("series", JSON.toJSONString(series));
		model.addAttribute("groupTypeName", MessageUtil.changMsg(qo));

		return "/customerReport/listByPie";
	}


	@RequestMapping("/listByBar")
	public String listByBar(@ModelAttribute("qo") CustomerReportQueryObject qo,Model model){

		PageInfo<HashMap> pageInfo = customerReportService.selectCustomerReport(qo);
		List<HashMap> list = pageInfo.getList();

		List<Object> x = new ArrayList<>();
		List<Object> y = new ArrayList<>();
		for (HashMap hashMap : list) {
			x.add(hashMap.get("groupType"));
			y.add(hashMap.get("number"));
		}

		model.addAttribute("x", JSON.toJSONString(x));
		model.addAttribute("y", JSON.toJSONString(y));
		model.addAttribute("groupTypeName", MessageUtil.changMsg(qo));

		return "/customerReport/listByBar";
	}


	@RequestMapping("/list")
	public String list(@ModelAttribute("qo") CustomerReportQueryObject qo,Model model){

		PageInfo<HashMap> pageInfo = customerReportService.selectCustomerReport(qo);
		model.addAttribute("pageInfo",pageInfo);

		return "/customerReport/list";
	}

}
